package util.apps;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Application for splitting a large text file into smaller files given a maximum number of lines.
 * 
 * @author nix
 *
 */
public class FileSplitter {
	//fields
	private File file;
	private int numLinesPerFile = 0;
	private int fileNumber = 1;
	
	/**This method will process each argument and assign any new varibles*/
	public void processArgs(String[] args){
		Pattern pat = Pattern.compile("-[a-z]");
		for (int i = 0; i<args.length; i++){
			String lcArg = args[i].toLowerCase();
			Matcher mat = pat.matcher(lcArg);
			if (mat.matches()){
				char test = args[i].charAt(1);
				try{
					switch (test){
					case 'f': file = new File(args[i+1]); i++; break;
					case 'n': numLinesPerFile=Integer.parseInt(args[i+1]); i++; break;
					case 'h': printDocs(); System.exit(0);
					default: System.out.println("\nProblem, unknown option! " + mat.group());
					}
				}
				catch (Exception e){
					System.out.print("\nSorry, something doesn't look right with this parameter request: -"+test);
					System.out.println();
					System.exit(0);
				}
			}
		}
		//check to see if they entered required params
		if (file==null || file.canRead() == false){
			System.out.println("\nCannot find your file!\n");
			System.exit(0);
		}
		if (numLinesPerFile == 0){
			System.out.println("\nEnter a maximum number of lines to place in each split file!\n");
			System.exit(0);
		}
	
	}
	public static void printDocs(){
		System.out.println("\n" +
				"**************************************************************************************\n" +
				"**                         File Splitter: June 2006                                 **\n" +
				"**************************************************************************************\n" +
				"Splits a big text file into smaller files given a maximum number of lines.\n\n"+
				
				"Required Parameters:\n"+
				"-f Full path file name for the text file.\n" +
				"-n Maximum number of lines to place in each split file.\n" +
	
				"\n" +
				"Example: java -Xmx256M -jar pathTo/T2/FileSplitter -f /affy/bpmap.txt -n 50000\n" +
				"\n" +
		"**************************************************************************************\n");
	}	
	public static void main(String[] args) {
		if (args.length !=4){
			printDocs();
			System.exit(0);
		}
		System.out.println("Launching FileSplitter...");
		new FileSplitter(args);
	}
	
	public FileSplitter(String[] args){
		processArgs(args);
		
		String line;
		try{
			BufferedReader in = new BufferedReader(new FileReader(file));
			PrintWriter out = getPrintWriter();
			int counter = 0;
			while ((line = in.readLine()) !=null) { 
				if (counter < numLinesPerFile) {
					out.println(line);
					counter++;
				}
				else {
					counter =1;
					out.close();
					out = getPrintWriter();
					out.println(line);
				}
			}
			//close final PrintWriter
			out.close();
		}
		catch (IOException e){
			e.printStackTrace();
		}
	}
	public PrintWriter getPrintWriter(){
		PrintWriter out =null;
		try {
			out = new PrintWriter(new FileWriter(file.getCanonicalPath()+fileNumber));
			fileNumber++;
		}
		catch (IOException e){
			e.printStackTrace();
		}
		return out;
	}
}

